﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using DTO;

namespace DAO
{
    public class HoaDonDAO:AbstractDAO
    {
        public static bool them(HoaDonDTO hoaDon)
        {
            bool ketQua = true;
            OleDbConnection ketNoi = null;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "INSERT INTO HoaDon(TenKhachHang,DiaChi,TriGia)";
                chuoiLenh += " VALUES (@TenKhachHang,@DiaChi,@TriGia)";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbParameter thamSo;
                thamSo = new OleDbParameter("@TenKhachHang", OleDbType.VarChar);
                thamSo.Value = hoaDon.TenKhachHang;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@DiaChi", OleDbType.VarChar);
                thamSo.Value = hoaDon.DiaChi;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@TriGia", OleDbType.Double);
                thamSo.Value = hoaDon.TriGia;
                lenh.Parameters.Add(thamSo);


                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ketQua = false;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return ketQua;
        }
        public static long layMaHoaDonLonNhat()
        {
            OleDbConnection ketNoi = null;
            long maHoaDonMax = 0;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "SELECT MAX(MaHoaDon) FROM HoaDon ";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbDataReader boDoc = lenh.ExecuteReader();
                while (boDoc.Read())
                {
                    maHoaDonMax = boDoc.GetInt32(0);

                }

            }
            catch
            {

            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return maHoaDonMax;
        }
    }
}
